﻿/*
* This software is licensed under the GNU General Public License, version 2
* You may copy, distribute and modify the software as long as you track changes/dates of in source files and keep all modifications under GPL. You can distribute your application using a GPL library commercially, but you must also provide the source code.

* DNNspot Software (http://www.dnnspot.com)
* Copyright (C) 2013 Atriage Software LLC
* Authors: Kevin Southworth, Matthew Hall, Ryan Doom

* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

* Full license viewable here: http://www.gnu.org/licenses/gpl-2.0.txt
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;

namespace DNNspot_CustomLogin
{
    public partial class Settings : ModuleSettingsBase
    {

        SettingsHelper settingsHelper = new SettingsHelper();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                if (ModuleSettings["txtLoginTemplate"] == null)
                    txtLoginTemplate.Text = settingsHelper.LoginTemplate;
                else
                    txtLoginTemplate.Text = Convert.ToString(ModuleSettings["txtLoginTemplate"]);

                if (ModuleSettings["txtLoggedInTemplate"] == null)
                    txtLoggedInTemplate.Text = settingsHelper.LoggedInTemplate;
                else
                    txtLoggedInTemplate.Text = Convert.ToString(ModuleSettings["txtLoggedInTemplate"]);

                if (ModuleSettings["txtFailedLogin"] == null)
                    txtFailedLogin.Text = settingsHelper.FailedLogin;
                else
                    txtFailedLogin.Text = Convert.ToString(ModuleSettings["txtFailedLogin"]);

                if (ModuleSettings["txtLockedOut"] == null)
                    txtLockedOut.Text = settingsHelper.LockedOut;
                else
                    txtLockedOut.Text = Convert.ToString(ModuleSettings["txtLockedOut"]);

                if (ModuleSettings["txtNotApproved"] == null)
                    txtNotApproved.Text = settingsHelper.NotApproved;
                else
                    txtNotApproved.Text = Convert.ToString(ModuleSettings["txtNotApproved"]);

                if (ModuleSettings["txtOtherLoginIssue"] == null)
                    txtOtherLoginIssue.Text = settingsHelper.OtherLoginErrors;
                else
                    txtOtherLoginIssue.Text = Convert.ToString(ModuleSettings["txtOtherLoginIssue"]);

                if (ModuleSettings["txtLoginButtonText"] == null)
                    txtLoginButtonText.Text = settingsHelper.LoginButtonText;
                else
                    txtLoginButtonText.Text = Convert.ToString(ModuleSettings["txtLoginButtonText"]);

                if (ModuleSettings["txtImageUrl"] == null)
                    txtImageUrl.Text = settingsHelper.LoginButtonImageUrl;
                else
                    txtImageUrl.Text = Convert.ToString(ModuleSettings["txtImageUrl"]);

                if (ModuleSettings["txtImageButtonWidth"] == null)
                    txtImageButtonWidth.Text = settingsHelper.LoginButtonImageWidth.ToString();
                else
                    txtImageButtonWidth.Text = Convert.ToString(ModuleSettings["txtImageButtonWidth"]);

                if (ModuleSettings["txtImageButtonHeight"] == null)
                    txtImageButtonHeight.Text = settingsHelper.LoginButtonImageHeight.ToString();
                else
                    txtImageButtonHeight.Text = Convert.ToString(ModuleSettings["txtImageButtonHeight"]);


                //ROLE 1 REDIRECT1
                DotNetNuke.Security.Roles.RoleController rc = new DotNetNuke.Security.Roles.RoleController();
                drpRoles1.Items.Add(new ListItem("", ""));
                drpRoles2.Items.Add(new ListItem("", ""));
                drpRoles3.Items.Add(new ListItem("", ""));
                drpRoles4.Items.Add(new ListItem("", ""));
                drpRoles5.Items.Add(new ListItem("", ""));
                foreach(DotNetNuke.Security.Roles.RoleInfo role in rc.GetPortalRoles(PortalId))
                {
                    //make the 5 drop down lists with the same roles
                    drpRoles1.Items.Add(new ListItem(role.RoleName, role.RoleName));
                    drpRoles2.Items.Add(new ListItem(role.RoleName, role.RoleName));
                    drpRoles3.Items.Add(new ListItem(role.RoleName, role.RoleName));
                    drpRoles4.Items.Add(new ListItem(role.RoleName, role.RoleName));
                    drpRoles5.Items.Add(new ListItem(role.RoleName, role.RoleName));
                }


                if (ModuleSettings["drpRoles1"] != null)
                    drpRoles1.SelectedValue = Convert.ToString(ModuleSettings["drpRoles1"]);
                if(ModuleSettings["txtRedirect1"] !=null)
                    txtRedirect1.Text=Convert.ToString(ModuleSettings["txtRedirect1"]);

                if (ModuleSettings["drpRoles2"] != null)
                    drpRoles2.SelectedValue = Convert.ToString(ModuleSettings["drpRoles2"]);
                if (ModuleSettings["txtRedirect2"] != null)
                    txtRedirect2.Text = Convert.ToString(ModuleSettings["txtRedirect2"]);

                if (ModuleSettings["drpRoles3"] != null)
                    drpRoles3.SelectedValue = Convert.ToString(ModuleSettings["drpRoles3"]);
                if (ModuleSettings["txtRedirect3"] != null)
                    txtRedirect3.Text = Convert.ToString(ModuleSettings["txtRedirect3"]);

                if (ModuleSettings["drpRoles4"] != null)
                    drpRoles4.SelectedValue = Convert.ToString(ModuleSettings["drpRoles4"]);
                if (ModuleSettings["txtRedirect4"] != null)
                    txtRedirect4.Text = Convert.ToString(ModuleSettings["txtRedirect4"]);

                if (ModuleSettings["drpRoles5"] != null)
                    drpRoles5.SelectedValue = Convert.ToString(ModuleSettings["drpRoles5"]);
                if (ModuleSettings["txtRedirect5"] != null)
                    txtRedirect5.Text = Convert.ToString(ModuleSettings["txtRedirect5"]);
                
                //hello

            }

        }

        public override void UpdateSettings()
        {
            ModuleController settings = new ModuleController();
            settings.UpdateModuleSetting(ModuleId, "txtLoginTemplate", txtLoginTemplate.Text);
            settings.UpdateModuleSetting(ModuleId, "txtLoggedInTemplate", txtLoggedInTemplate.Text);
            settings.UpdateModuleSetting(ModuleId, "txtFailedLogin", txtFailedLogin.Text);
            settings.UpdateModuleSetting(ModuleId, "txtLockedOut", txtLockedOut.Text);
            settings.UpdateModuleSetting(ModuleId, "txtNotApproved", txtNotApproved.Text);
            settings.UpdateModuleSetting(ModuleId, "txtOtherLoginIssue", txtOtherLoginIssue.Text);
            settings.UpdateModuleSetting(ModuleId, "txtLoginButtonText", txtLoginButtonText.Text);
            settings.UpdateModuleSetting(ModuleId, "txtImageUrl", txtImageUrl.Text);
            settings.UpdateModuleSetting(ModuleId, "txtImageButtonWidth", txtImageButtonWidth.Text);
            settings.UpdateModuleSetting(ModuleId, "txtImageButtonHeight", txtImageButtonHeight.Text);
            settings.UpdateModuleSetting(ModuleId, "drpRoles1", drpRoles1.SelectedValue.ToString());
            settings.UpdateModuleSetting(ModuleId, "txtRedirect1", txtRedirect1.Text);
            settings.UpdateModuleSetting(ModuleId, "drpRoles2", drpRoles2.SelectedValue.ToString());
            settings.UpdateModuleSetting(ModuleId, "txtRedirect2", txtRedirect2.Text);
            settings.UpdateModuleSetting(ModuleId, "drpRoles3", drpRoles3.SelectedValue.ToString());
            settings.UpdateModuleSetting(ModuleId, "txtRedirect3", txtRedirect3.Text);
            settings.UpdateModuleSetting(ModuleId, "drpRoles4", drpRoles4.SelectedValue.ToString());
            settings.UpdateModuleSetting(ModuleId, "txtRedirect4", txtRedirect4.Text);
            settings.UpdateModuleSetting(ModuleId, "drpRoles5", drpRoles5.SelectedValue.ToString());
            settings.UpdateModuleSetting(ModuleId, "txtRedirect5", txtRedirect5.Text);

        }
    }
}